package com.inmobi.re.container.mraidimpl;

import android.media.AudioRecord;
import com.inmobi.commons.internal.Log;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class AudioTriggerer
{
  static boolean a;
  public static AudioRecord audioRecorder = null;
  static boolean b;
  static Timer c;
  public static List<AudioTriggerCallback> callbacks = new ArrayList();
  static long d = 0L;
  static long e = 50L;
  private static int[] f = { 8000, 11025, 22050, 44100 };

  private static void a(double paramDouble)
  {
    Iterator localIterator = callbacks.iterator();
    while (localIterator.hasNext())
    {
      AudioTriggerCallback localAudioTriggerCallback = (AudioTriggerCallback)localIterator.next();
      try
      {
        localAudioTriggerCallback.audioLevel(paramDouble);
      }
      catch (Exception localException)
      {
        Log.internal("IMRE_3.7.0", "AudioTriggerer: One of the mic listeners has a problem.");
      }
    }
  }

  public static void addEventListener(AudioTriggerCallback paramAudioTriggerCallback)
  {
    callbacks.add(paramAudioTriggerCallback);
    if (callbacks.size() == 1)
      b();
  }

  private static void b()
  {
    a = true;
    audioRecorder = h();
    c = new Timer();
    c.scheduleAtFixedRate(new a(), d, e);
  }

  private static void c()
  {
    a = false;
    if (audioRecorder != null)
      if (f())
        g();
    try
    {
      audioRecorder.stop();
      audioRecorder.release();
      c.cancel();
      c.purge();
      return;
    }
    catch (IllegalStateException localIllegalStateException)
    {
      localIllegalStateException.printStackTrace();
      return;
    }
    catch (RuntimeException localRuntimeException)
    {
      localRuntimeException.printStackTrace();
    }
  }

  private static void d()
  {
    if (audioRecorder == null);
    while (audioRecorder.getState() != 1)
      return;
    short[] arrayOfShort = new short[512];
    float[] arrayOfFloat = new float[3];
    b = true;
    try
    {
      audioRecorder.startRecording();
      while (f())
      {
        int i = audioRecorder.read(arrayOfShort, 0, arrayOfShort.length);
        int j = 0;
        float f1 = 0.0F;
        while (j < i)
        {
          int m = (short)(arrayOfShort[j] | arrayOfShort[(j + 1)]);
          if (m != 0)
            f1 += Math.abs(m) / i;
          j += 2;
        }
        arrayOfFloat[0] = f1;
        int k = 0;
        float f2 = 0.0F;
        while (k < 3)
        {
          f2 += arrayOfFloat[k];
          k++;
        }
        a(f2 / i / 32.0F);
      }
      e();
      return;
    }
    catch (Exception localException)
    {
    }
  }

  private static void e()
  {
    if (audioRecorder != null)
      if (f())
        g();
    try
    {
      audioRecorder.stop();
      audioRecorder.release();
      return;
    }
    catch (IllegalStateException localIllegalStateException)
    {
      localIllegalStateException.printStackTrace();
      return;
    }
    catch (RuntimeException localRuntimeException)
    {
      localRuntimeException.printStackTrace();
    }
  }

  private static boolean f()
  {
    return b;
  }

  private static void g()
  {
    b = false;
  }

  private static AudioRecord h()
  {
    for (int k : f)
      for (int i1 : new short[] { 3, 2 })
      {
        short[] arrayOfShort2 = { 16, 12 };
        int i2 = arrayOfShort2.length;
        int i3 = 0;
        while (i3 < i2)
        {
          int i4 = arrayOfShort2[i3];
          try
          {
            int i5 = AudioRecord.getMinBufferSize(k, i4, i1);
            if (i5 != -2)
            {
              AudioRecord localAudioRecord = new AudioRecord(0, k, i4, i1, i5);
              int i6 = localAudioRecord.getState();
              if (i6 == 1)
                return localAudioRecord;
            }
          }
          catch (Exception localException)
          {
            i3++;
          }
        }
      }
    return null;
  }

  public static void removeEventListener(AudioTriggerCallback paramAudioTriggerCallback)
  {
    callbacks.remove(paramAudioTriggerCallback);
    if (callbacks.size() == 0)
      c();
  }

  static final class a extends TimerTask
  {
    public void run()
    {
      AudioTriggerer.a();
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.inmobi.re.container.mraidimpl.AudioTriggerer
 * JD-Core Version:    0.6.2
 */